from django.contrib import admin
from django.contrib.sites.models import Site
from mitems.models import UserProfile, Item, Item_category,\
Item_group, Item_type, Comment, Like, Share, Follower,\
ItemImage, Airtime, Inquestion, Notification
from persistent_messages.models import Message
from django.contrib.auth.models import User, Group
from django.core.cache import cache


class LikeInline(admin.TabularInline):

    model = Like
    extra = 1

class Inquestioninline(admin.TabularInline):

    model = Inquestion
    extra = 1

class CommentLikeInline(admin.TabularInline):

    model = Like
    extra = 1


class CommentInline(admin.TabularInline):

    model = Comment
    extra = 1
    inlines = [CommentLikeInline]


class ShareInline(admin.TabularInline):

    model = Share
    extra = 1


class ItemImageInline(admin.TabularInline):

    model = ItemImage
    extra = 1


class AirtimeInline(admin.TabularInline):

    model = Airtime
    extra = 1


class NotificatioinInline(admin.TabularInline):
    model = Notification


class MessageInline(admin.TabularInline):

    model = Message
    extra = 1
    fk_name = 'user'


class ItemAdmin(admin.ModelAdmin):

    inlines = (CommentInline, LikeInline, ShareInline, AirtimeInline,
               ItemImageInline, Inquestioninline)
    exclude = ('created_by', 'updated_by',)
    ordering = ('-updated',)
    date_hierarchy = 'updated'
    readonly_fields = (
        'actions_total_visitors',
        'actions_total_foreign',
        'impact',
        'signal_strength',
        'time_expired',
        'actions_total',
        'more_cycle',
        'time_total',
        'unique_users',
        'slumped'
    )

    list_display = ('message', 'get_absolute_url', 'slumped',
                    'more_cycle', 'time_total', 'impact',
                    'signal_strength', 'score', 'created',
                    'time_expired'
    )

    def save_model(self, request, obj, form, change):
        obj.updated_by = request.user
        if not change:
            obj.created_by = request.user
        obj.save()
        cache.clear()

    def queryset(self, request):
        qs = Item.admin.all()
        return qs


class FollowerInline(admin.TabularInline):

    model = Follower
    extra = 1
    fk_name = 'follower'


class UserProfileAdmin(admin.ModelAdmin):

    inlines = (FollowerInline, AirtimeInline, Inquestioninline, NotificatioinInline, MessageInline)
    exclude = ('password',)
    readonly_fields = (
        'airtime_avg',
        'last_login',
        'date_joined'
    )
    list_display = ('username', 'get_absolute_url', 'email', 'mobile_email', 'date_joined',
                    'last_login', 'location', 'is_active', 'is_bot')


admin.site.register(Item, ItemAdmin)
admin.site.register(UserProfile, UserProfileAdmin)
admin.site.register(Item_category)
admin.site.register(Item_group)
admin.site.register(Item_type)
admin.site.register(Comment)
admin.site.register(Like)
admin.site.register(Share)
admin.site.register(Follower)
admin.site.register(Airtime)
#admin.site.unregister(User)
admin.site.unregister(Group)
admin.site.unregister(Site)
